import 'package:get/get.dart';
import 'package:honeypotflutter/modules/cabinet/api/cabinetApi.dart';
import '../../model/cabinet_info_model.dart';
import '../../model/cabinet_info_status_model.dart';
import '../../model/cabinet_list_model.dart';

class CabinetListInfoLogic extends GetxController {
  late CabinetListModel itemModel;
  List<CabinetInfoModel> infoModelList = [];
  List<CabinetInfoStatusModel> statusModelList = [];

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    itemModel = Get.arguments ?? CabinetListModel();
    request();
  }

  /*
  * 数据请求
  * */
  request() async {
    if (itemModel.id == null) {
      return;
    }
    await Future.wait([CabinetApi.cabinetInfo(itemModel.id!), CabinetApi.cabinetStatus(itemModel.id!)]).then((value) {
      infoModelList = value[0] as List<CabinetInfoModel>;
      statusModelList = value[1] as List<CabinetInfoStatusModel>;
      update();
    }).catchError((error) {});
  }

  /*
  * 查找
  * */
  CabinetInfoStatusModel? findStatus(int? boardNo) {
    if (boardNo == null) {
      return null;
    }
    List<CabinetInfoStatusModel> result = statusModelList.where((e) => e.boardNo == boardNo).toList();
    if (result.isEmpty) {
      return null;
    }
    return result.first;
  }
}
